<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/24 0024
 * Time: 19:46
 */
namespace app\admin\controller;
use think\Controller;
use app\common\model\News;
use think\image\Exception;

class Content extends Common {

    public function index (){
        $conds = array();
        if(isset($_GET['title']) && $_GET['title']) {
            $conds['title'] = $_GET['title'];
            $typeTitle = $_GET['title'];
            $this->assign('typeTitle',$typeTitle);
        } else {
            $this->assign('typeTitle','');
        }
        if(isset($_GET['catid']) && $_GET['catid']) {
            $conds['catid'] = intval($_GET['catid']);
            $typeCatid = intval($_GET['catid']);
            $this->assign('typeCatid',$typeCatid);
        } else {
            $this->assign('typeCatid',-100);
        }
        $conds['status'] = array('neq',-1);
        $news = model('news')->getNews($conds);

        $postions = model("position")->getNormalPositions();
        $this->assign('positions', $postions);
        $this->assign('pageres',$news);
        $this->assign('webSiteMenu',model('menu')->getBarMenus());
        return $this->fetch();
    }
    public function add(){
        if ($_POST){
            if (!isset($_POST['title']) || !$_POST['title']) {
                return show(0, '标题不存在');
            }
            if (!isset($_POST['small_title']) || !$_POST['small_title']) {
                return show(0, '短标题不存在');
            }
            if (!isset($_POST['catid']) || !$_POST['catid']) {
                return show(0, '文章栏目不存在');
            }
            if (!isset($_POST['keywords']) || !$_POST['keywords']) {
                return show(0, '关键字不存在');
            }
            if (!isset($_POST['content']) || !$_POST['content']) {
                return show(0, 'content不存在');
            }
            if(isset($_POST['news_id']) && $_POST['news_id']) {
                return $this->saveData($_POST);
            }
            //$newsCount = model('news')->insert($_POST);
            $news = new News;
            $news->insert($_POST);
            $newsId = $news->news_id;
            if ($newsId) {
                $newsContentData['content'] = $_POST['content'];
                $newsContentData['news_id'] = $newsId;
                $cId = model('news_content')->insert($newsContentData);
                if($cId){
                    return show(1, '新增成功');
                } else {
                    return show(1, '主表插入成功，副表插入失败');
                }
            } else {
                return show(0, '新增失败');
            }
            
        } else {
            $webSiteMenu = model('menu')->getBarMenus();
            $titleFontColor = config('TITLE_FONT_COLOR');
            $copyFrom = config('COPY_FROM');
            return $this->fetch('',[
                'webSiteMenu' => $webSiteMenu,
                'titleFontColor' => $titleFontColor,
                'copyFrom' => $copyFrom
            ]);
        }
    }

    public function edit(){
        $newsId = input('get.id');
        if(!$newsId) {
            //执行跳转
            $this->redirect('/admin/content');
        }
        $news = model('news')->find($newsId);
        if(!$news) {
            $this->redirect('/admin/content');
        }
        $newsContent = model('news_content')->find($newsId);
        if($newsContent) {
            $news['content'] = $newsContent['content'];
        }

        $webSiteMenu = model('menu')->getBarMenus();

        return $this->fetch('',[
            'webSiteMenu' => $webSiteMenu,
            'titleFontColor' => config('TITLE_FONT_COLOR'),
            'copyFrom' => config('COPY_FROM'),
            'news' => $news
        ]);
    }

    public function saveData($data) {
        $newsId = $data['news_id'];
        unset($data['news_id']);
        try {
            $id = model('news')->updateById($newsId, $data);
            $newsContentData['content'] = $data['content'];
            $condId = model('news_content')->updateNewsById($newsId, $newsContentData);
            if($id === false || $condId === false) {
                return show(0, '更新失败');
            }
            return show(1, '更新成功');
        } catch (Exception $e) {
            return show(0, $e->getMessage());
        }
    }

    public function setStatus (){
        try {
            if($_POST) {
                $id = $_POST['id'];
                $status = $_POST['status'];

                if(!$id) {
                    return show(0, 'ID不存在');
                }
                $res = model('news')->updateStatusById($id, $status);
                if($res) {
                    return show(1, '操作成功');
                }
                return show(0, '操作失败');
            }
            return show(0, '没有提交内容');
        } catch (Exception $e) {
            return show(0, $e->getMessage());
        }
    }

    public function listorder (){
        $listorder = $_POST['listorder'];
        $jumpUrl = $_SERVER['HTTP_REFERER'];
        $errors= array();
        try{
            if($listorder) {
                foreach ($listorder as $newsId=> $v) {
                    //执行更新
                    $id = model('news')->updateNewsLisorderById($newsId, $v);
                    if($id === false) {
                        $errors[] = $newsId;
                    }
                }
                if ($errors) {
                    return show(0,'排序失败-'.implode(',',$errors), array('jump_url'=>$jumpUrl));
                }
                return show(1, '排序成功', array('jump_url'=>$jumpUrl));
            }
        } catch (Exception $e) {
            return show(0, $e->getMessage());
        }
        return show(0, '排序数据失败', array('jump_url'=>$jumpUrl));
    }

    public function push(){
        $jumpUrl = $_SERVER['HTTP_REFERER'];
        $positionId = intval($_POST['position_id']);
        $newsId = $_POST['push'];

        if(!$newsId || !is_array($newsId)) {
            return show(0, '请选择推荐位的文章ID进行推荐');
        }
        if(!$positionId) {
            return show(0, '没有选择推荐位');
        }

        try {

            $news = model('news')->getNewsByNewsIdIn($newsId);

            if(!$news) {
                return show(0, "没有相关内容");
            }

            foreach ($news as $new) {
                $data = array(
                    'position_id' => $positionId,
                    'title' => $new['title'],
                    'thumb' => $new['thumb'],
                    'news_id' => $new['news_id'],
                    'status' => 1,
                    'create_time' => strtotime($new['create_time'])
                );
                model('positionContent')->insertData($data);
            }
        }catch (Exception $e) {
            return show(0, $e->getMessage());
        }
        return show(1, '推荐成功', array('jump_url'=>$jumpUrl));
    }
}